<?php

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$taskid=intval($_RQ['taskid']);
	$timeid=intval($_RQ['timeid']);
	$roomid=intval($_RQ['roomid']);
	$get_goods=$this->model('model')->get_goods($taskid,$roomid,$timeid);
	/*$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'timeid'=>$timeid,'roomid'=>$roomid,'taskid'=>$taskid);
    $fields='';
    $orderby='id desc';
	$total='';
	$goods=pdo_getslice('jy_seckill_task_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($goods as &$v){
		$good=pdo_get('jy_weishop_goods',array('id'=>$v['goodsid']),array('title','image','price'));
		$v['image']=$good['image'];
		$v['timeid']=$v['timeid'].'点';
		$v['title']=$good['title'];
		$v['marketprice']=$good['price'];
		$v['seckillinfo'] = $this->model('model')->getSeckill($v['goodsid']);		
		$v['room']=pdo_getcolumn('jy_seckill_task_room',array('id'=>$v['roomid']),'title');
		$v['task']=pdo_getcolumn('jy_seckill_task',array('id'=>$v['taskid']),'title');
	}*/
	$return['list']=$get_goods['goods'];
	$return['time']=$get_goods['time'];
	$return['times']=$get_goods['validtimes'];
	foreach($return['list'] as &$itemv){
			$itemv['carttotal']=pdo_getcolumn('jy_weishop_member_cart',array('goodsid'=>$itemv['goodsid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'total');
			$itemv['cartid']=pdo_getcolumn('jy_weishop_member_cart',array('goodsid'=>$itemv['goodsid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
			$itemv['cartid']=$itemv['cartid']?:0;
			$itemv['carttotal']=$itemv['carttotal']?:0;
			$itemv['optionsset']=$itemv['optionsset']?json_decode($itemv['optionsset'],true):array();
			if(count($itemv['optionsset'])>0){
				$itemv['isoptions']=1;
			}
	}
	if((!$taskid || !$roomid) && $return['times']){
		$currenttime = time();
		foreach ($return['times'] as $key => &$time) {
			$timestr = $time['time'];
			if (strlen($timestr) == 1) {
				$timestr = '0' . $timestr;
			}
			$starttime = strtotime(date('Y-m-d ' . $timestr . ':00:00'));
			if (isset($return['times'][$key + 1])) {
				$end = $return['times'][$key + 1]['time'] - 1;
				$endtime = strtotime(date('Y-m-d ' . $end . ':59:59'));
			}else {
				$endtime = strtotime(date('Y-m-d 23:59:59'));
			}
			if($setting['maxhours']>=1){
				$settime=$setting['maxhours']*3600+$starttime;
				$endtime=$settime<$endtime?$settime:$endtime;
			}
			$time['endtime'] = $endtime;
			$time['starttime'] = $starttime;
			$time['endtime2'] = date('Y-m-d H',$endtime);
			$time['starttime2'] = date('Y-m-d H',$starttime);
			$time['etime'] = $endtime-time();
			$time['stime'] = $starttime-time();
			$time['time'] = $timestr;
			if (($starttime <= $currenttime) && ($currenttime <= $endtime)) {
				$time['status'] = 1;
				$timeid = $time['time'];
				if ($timeindex == -1) {
					$timeindex = $key;
				}
				$time['rest_time']=model('jy_weishop/model')->date($endtime-TIMESTAMP);
				$time['rest_time2']=$endtime-TIMESTAMP;		
				$times[] = $time;
			}else if ($currenttime < $starttime) {
				$time['status'] = 0;
				if (empty($timeid)) {
					$timeid = $time['time'];
				}
				$time['rest_time']=model('jy_weishop/model')->date($starttime-TIMESTAMP);
				$time['rest_time2']=$starttime-TIMESTAMP;
				$times[] = $time;
			}else {
				if ($endtime < $currenttime) {
					$time['status'] = -1;
					if (empty($timeid)) {
						$timeid = $time['time'];
					}
				}
			}
		}
	}
	$return['roomid']=$roomid?:$get_goods['roomid'];
	$return['taskid']=$taskid?:$get_goods['taskid'];
	$return['timeid']=$timeid?:$get_goods['timeid'];
	exi($return);
}